package com.gateway.security.endpoint.claim;

import com.gateway.common.dto.PluginData;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.collect.Maps;
import reactor.core.publisher.Mono;

import java.util.Map;

/**
 * @program: api-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2025/2/12 22:13
 **/
public class ClaimDataCache {

    private final static ClaimDataCache INSTANCE = new ClaimDataCache();

    private final static Map<String, LoadingCache<String, Mono<Map<String, Object>>>> CLAIMS_CACHE = Maps.newConcurrentMap();

    private ClaimDataCache() {}

    public static ClaimDataCache getInstance() {
        return INSTANCE;
    }

    public Map<String, LoadingCache<String, Mono<Map<String, Object>>>> getClaimsCache() {
        return CLAIMS_CACHE;
    }

    /**
     * 缓存
     * @param key
     * @param cache
     */
    public void cache(String key, LoadingCache<String, Mono<Map<String, Object>>> cache) {
        CLAIMS_CACHE.put(key, cache);
    }

    /**
     * Clean
     */
    public void clean() {
        CLAIMS_CACHE.clear();
    }

    /**
     * Obtain LoadingCache
     *
     * @param key the key
     * @return the LoadingCache
     */
    public LoadingCache<String, Mono<Map<String, Object>>> obtain(final String key) {
        return CLAIMS_CACHE.get(key);
    }

    /**
     * Remove
     * @param key
     */
    public boolean remove(final String key) {
        return CLAIMS_CACHE.remove(key) != null;
    }

    /**
     * Existed
     * @param key
     * @return
     */
    public boolean existed(final String key) {
        return CLAIMS_CACHE.containsKey(key);
    }


}
